﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace Hetang.Models.ControlExtensions
{
    public static class LabelExtensions
    {
        public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object htmlAttributes)
        {
            return html.LabelFor(expression, null, htmlAttributes);
        }

        public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string labelText, object htmlAttributes)
        {
            return html.LabelHelper(
                ModelMetadata.FromLambdaExpression(expression, html.ViewData),
                ExpressionHelper.GetExpressionText(expression),
                HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes),
                labelText);
        }

        private static MvcHtmlString LabelHelper(this HtmlHelper html, ModelMetadata metadata, string htmlFieldName, IDictionary<string, object> htmlAttributes, string labelText = null)
        {
            var str = labelText
                ?? (metadata.DisplayName
                ?? (metadata.PropertyName
                ?? htmlFieldName.Split(new[] { '.' }).Last()));

            if (string.IsNullOrEmpty(str))
                return MvcHtmlString.Empty;

            var tagBuilder = new TagBuilder("label");
            tagBuilder.MergeAttributes(htmlAttributes);
            tagBuilder.Attributes.Add("for", TagBuilder.CreateSanitizedId(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName)));
            tagBuilder.SetInnerText(str);

            return tagBuilder.ToMvcHtmlString(TagRenderMode.Normal);
        }

        private static MvcHtmlString ToMvcHtmlString(this TagBuilder tagBuilder, TagRenderMode renderMode)
        {
            return new MvcHtmlString(tagBuilder.ToString(renderMode));
        }        
    }

    public static class StylesheetExtensions
    {
        /// <summary>
        /// 获取自定义样式表
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static MvcHtmlString GetCustomStylesheet(this HtmlHelper htmlHelper, string name)
        {
            TagBuilder tagBuilder = new TagBuilder("Link");
            tagBuilder.Attributes.Add("rel", "stylesheet");
            tagBuilder.Attributes.Add("type", "text/css");
            tagBuilder.Attributes.Add("href", VirtualPathUtility.ToAbsolute("~/data/css/" + name + "?v=" + FileHelper.GetFileCreateDateTime("~/data/css/" + name)));
            //创建经过HTML加密的字符串
            //TagRenderMode.SelfClosing枚举值代表当前标签是自动关闭的
            return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.SelfClosing));
        }

        /// <summary>
        /// 获取样式表
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static MvcHtmlString GetStylesheet(this HtmlHelper htmlHelper, string name)
        {
            TagBuilder tagBuilder = new TagBuilder("Link");
            tagBuilder.Attributes.Add("rel", "stylesheet");
            tagBuilder.Attributes.Add("type", "text/css");
            tagBuilder.Attributes.Add("href", VirtualPathUtility.ToAbsolute("~/Content/" + name));
            //创建经过HTML加密的字符串
            //TagRenderMode.SelfClosing枚举值代表当前标签是自动关闭的
            return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.SelfClosing));
        }
    }

    public static class JavascriptExtensions
    {
        /// <summary>
        /// 获取自定义javascript文件
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static MvcHtmlString GetCustomJavascript(this HtmlHelper htmlHelper, string name)
        {
            TagBuilder tagBuilder = new TagBuilder("script");
            tagBuilder.Attributes.Add("type", "text/javascript");
            tagBuilder.Attributes.Add("src", VirtualPathUtility.ToAbsolute("~/data/js/" + name + "?v=" + FileHelper.GetFileCreateDateTime("~/data/js/" + name)));
            //创建经过HTML加密的字符串
            //TagRenderMode.SelfClosing枚举值代表当前标签是自动关闭的
            return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.Normal));
        }

        /// <summary>
        /// 获取系统javascript文件
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static MvcHtmlString GetJavascript(this HtmlHelper htmlHelper, string name)
        {
            TagBuilder tagBuilder = new TagBuilder("script");
            tagBuilder.Attributes.Add("type", "text/javascript");
            tagBuilder.Attributes.Add("src", VirtualPathUtility.ToAbsolute("~/Scripts/" + name));
            //创建经过HTML加密的字符串
            //TagRenderMode.SelfClosing枚举值代表当前标签是自动关闭的
            return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.Normal));
        }
    }

    public static class FileHelper
    {
        /// <summary>
        /// 获取文件最后修改时间
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static string GetFileCreateDateTime(string path)
        {
            FileInfo f = new FileInfo(HttpContext.Current.Server.MapPath(path));
            return string.Format("{0}{1}{2}", f.LastWriteTime.Year, f.LastWriteTime.Month, f.LastWriteTime.Day);
        }
    }
}